using System.Collections.Specialized;
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Be.Timvw.Framework.Tests.Configuration.ConfigurationFileSession
{
    /// <summary>
    /// Specifications for configuration file session.
    /// </summary>
    [TestClass]
    public class WhenUsingConfigurationFileSession
    {
        private const string AppSettingsKey = "ConfigKey";
        private const string AppSettingsValue = "ConfigValue";
        private const string ConfigFile = "ConfigurationFileSession.config";

        private const string OtherConfigKey = "OtherConfigKey";
        private const string OtherConfigValue = "OtherConfigValue";
        private const string OtherSection = "OtherSection";

        /// <summary>
        /// In a session the configuration manager should expose the values defined in the loaded configuration file.
        /// </summary>
        [TestMethod]
        public void ShouldHaveLoadedConfigurationFileValues()
        {
            using(new Framework.Configuration.ConfigurationFileSession(ConfigFile))
            {
                Assert.AreEqual(AppSettingsValue, ConfigurationManager.AppSettings[AppSettingsKey]);

                NameValueCollection dictionary = (NameValueCollection) ConfigurationManager.GetSection(OtherSection);
                Assert.AreEqual(OtherConfigValue, dictionary[OtherConfigKey]);
            }
        }

        /// <summary>
        /// After dispose of the session, the original values should be restored.
        /// </summary>
        [TestMethod]
        public void ShouldRestoreOriginalValuesAfterDispose()
        {
            using(new Framework.Configuration.ConfigurationFileSession(ConfigFile))
            {
            }
            Assert.IsNull(ConfigurationManager.AppSettings[AppSettingsKey]);
            NameValueCollection dictionary = (NameValueCollection) ConfigurationManager.GetSection(OtherSection);
            Assert.IsNull(dictionary[OtherConfigKey]);
        }
    }
}